package Service;

import Controller.loginController;
import Data.event;
import MyInterfaces.MassageType;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
public class Main extends Application {


    public static Stage primaryStage;
    public static Socket socket;

    public static GetContronller AllContronller=new GetContronller();

    public static GetContronller getAllContronller() {
        return AllContronller;
    }

    @Override
    public void start(Stage stage) throws Exception {
        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\login.fxml");
    //    System.out.println(file.getAbsolutePath());
        URL location=file.toURI().toURL();
        FXMLLoader fxmlLoader=new FXMLLoader();
        fxmlLoader.setLocation(location);

        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        primaryStage=stage;
        Parent root = fxmlLoader.load();
        primaryStage.setTitle("welcome to here");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root,600,400));
        primaryStage.show();
        AllContronller.setLoginController(fxmlLoader.getController());

        socket=new Socket("127.0.0.1",3684);

        SendMassage.setSocket(socket);
        GetMassage.setSocket(socket);

        loginController.init(socket);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                try {
                    event data=new event();
                    data.setType(MassageType.Exit);
                    SendMassage send=new SendMassage();
                    send.sendMassage(data);
                    System.exit(0);
                    socket.close();
                    stage.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                //    System.out.print("监听到窗口关闭");
            }
        });

    }

    public static void main(String[] args) throws Exception {

      launch(args);
    }

}